<?php
class App {
  private $data;
  
  public function __construct($sys, $orm) {
    $this->data = new stdClass();
    
    if (!isset($_SESSION[SYS . $sys])) {
      $opts = new stdClass();
      $opts->cd = 't.php_name = :v1';
      $opts->cdv = array(':v1' => $sys);
      $opts->sel = 't.*, t1.folder AS __folder';
      $opts->jt = 'LEFT JOIN pb_theme AS t1 ON t1.id = t.theme_id';
      $rslt= $orm->get('pb_sys', NULL, NULL, NULL, NULL, $opts)->d;
      if (count($rslt) == 0) {
        die('No System Found');
      }
      $this->data->sys = $rslt[0];
      
      if ($this->data->sys->theme_id == 0) {
        die('No Theme Selected');
      }
      
      $opts = new stdClass();
      $opts->cd = 't.sys_id = :v1';
      $opts->cdv = array(':v1' => $this->data->sys->id);
      $opts->sel = 't.id, t.parent_id, t.name, t.order, t.is_default, t.is_visible, t.sys_id, t.icon';
      $rslt = $orm->get('pb_page', 't.order', 'ASC', NULL, NULL, $opts)->d;
      
      $this->data->pages= array();
      $this->data->__login_id = 0;
      $this->data->__default_id = 0;
      foreach ($rslt as &$itm) {
        if ($itm->is_default == 1) {
          $this->data->__default_id = $itm->id;
        }
        if ($itm->id == $this->data->sys->login_id) {
          $this->data->__login_id = $itm->id;
        }
        if ($itm->is_visible == 1) {
          $this->data->pages[] = $itm;
        }
      }
      
      if ($this->data->sys->login_required == 1 && $this->data->__login_id == 0) {
        die('No Login Page Found');
      }
      
      if ($this->data->__default_id == 0) {
        die('No Default Page Found');
      }
      
      $this->data->exceptions = array();
      $this->data->plugins = array();
      $this->data->instances = array();
      $this->data->__instances = array();
      
      $opts = new stdClass();
      $opts->cd = 't.sys_id = :v1';
      $opts->cdv = array(':v1' => $this->data->sys->id);
      $opts->sel = 't.*, t1.folder AS __folder';
      $opts->jt = 'LEFT JOIN pb_plugin AS t1 ON t1.id = t.plugin_id';
      $rslt = $orm->get('pb_plugin_instance', NULL, NULL, NULL, NULL, $opts)->d;
      foreach ($rslt as &$itm) {
        $this->data->instances[$itm->id] = $itm;
        
        if (!isset($this->data->__instances[$itm->page_id])) {
          $this->data->__instances[$itm->page_id] = array();
        }
        $this->data->__instances[$itm->page_id][] = $itm;
        
        $this->data->plugins[] = $itm->__folder;
        if ($itm->page_id == $this->data->__login_id && !in_array($itm->__folder, $this->data->exceptions)) {
          $this->data->exceptions[] = $itm->__folder;
        }
      }
      $_SESSION[SYS . $sys] = $this->data;
    } else {
      $this->data = $_SESSION[SYS . $sys];
    }
    
    $this->data->param_logout = Utils::get_param('logout', NULL);
    $this->data->param_url = Utils::get_param('u', NULL);
    $this->data->param_id = Utils::get_param('i', 0);
    $this->data->param_file = Utils::get_param('__file', 'index');
    
    $query = $_SERVER['QUERY_STRING'];
    foreach ($_POST as $key => $val) {
      if ($query != '') {
        $query .= '&';
      }
      $query .= $key . '=' . $val;
    }
    $this->data->query = Utils::convert($query);
//    Utils::dump($this->data->query);
    
    if ($this->data->sys->login_required == 1) {
      if ($this->data->param_logout) {
        unset($this->data->is_authorised);
        header('location: ' . URL . HOME_DIR . $sys . '.php?i=' . $this->data->__login_id);
        return;
      } 
      
      if ($this->data->param_id != $this->data->__login_id && !isset($this->data->is_authorised)) {
        header('location: ' . URL . HOME_DIR . $sys . '.php?i=' . $this->data->__login_id . '&u=' . urlencode(Utils::get_url()));
        return;
      } 
      
      if ($this->data->param_id == $this->data->__login_id && isset($this->data->is_authorised)) {
        $this->data->param_url = ($this->data->param_url) ? $this->data->param_url :  URL . HOME_DIR . $sys . '.php?i=' . $this->data->__default_id;
        header('location: ' . $this->data->param_url);
        return;
      }
    }
    
    if (!isset($this->data->contents)) {
      $this->data->contents = array();
    }
    
    $this->data->param_id = ($this->data->param_id == 0) ? $this->data->__default_id : $this->data->param_id;
    if (!isset($this->data->contents[$this->data->query])) {
      $opts = new stdClass();
      $opts->cd = 't.id = :v1';
      $opts->cdv = array(':v1' => $this->data->param_id);
      $rslt = $orm->get('pb_page', NULL, NULL, NULL, NULL, $opts)->d;
      if (count($rslt) == 0) {
        die('No Page Found');
      }
      
      $this->data->contents[$this->data->query] = $rslt[0];
      
      $insts = isset($this->data->__instances[$rslt[0]->id]) ? $this->data->__instances[$rslt[0]->id] : array();
      $content = str_replace('&nbsp;', '', strip_tags($this->data->contents[$this->data->query]->html));
      if (count($insts) == 1 && trim($content) == Utils::get_inst_cd($insts[0])) {
        $this->data->contents[$this->data->query]->__inst = $insts[0]->id;
        $this->data->contents[$this->data->query]->html = PLGN_DIR . $insts[0]->__folder . '/' . Utils::get_param('__file', 'index') . '.php';
      } else {
        $this->data->contents[$this->data->query]->__iframe = 1;
        
        foreach ($insts as &$itm) {
          $this->data->contents[$this->data->query]->html = str_replace('<p>' . Utils::get_inst_cd($itm) . '</p>', $this->__plgn($itm, $sys, $query), $this->data->contents[$query]->html);
          $this->data->contents[$this->data->query]->html = str_replace(Utils::get_inst_cd($itm), $this->__plgn($itm, $sys, $query), $this->data->contents[$query]->html);
        }
      }
      $_SESSION[SYS . $sys] = $this->data;
    }
  }
  
  public function get_data() {
    return $this->data;
  }
  
  public function __plgn($inst, $sys, $query) {
    return "<script>function plugin_$inst->id() {  Utils.plugin(\"plgnifm_$inst->id\"); setTimeout(function() { Utils.plugin(\"plgnifm_$inst->id\"); }, 500) }</script>
            <iframe id=\"plgnifm_$inst->id\" onload=\"Javascript:plugin_$inst->id();\" style=\"width: $inst->width; height: $inst->height\" scrolling=\"no\" frameborder=\"0\"  marginwidth=\"0\" marginheight=\"0\" allowtransparency=\"true\" src=\"__plgn.php?__sys=$sys&amp;__inst=$inst->id&amp;" . str_replace('&', '&amp;', $query) . "\"></iframe>";
  }
}
?>